package org.jeecg.modules.arc.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.YouBianCodeUtil;
import org.jeecg.modules.arc.entity.ArcCustomers;
import org.jeecg.modules.arc.service.IArcCustomersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 用户水表
 * @Author: jeecg-boot
 * @Date:   2022-02-07
 * @Version: V1.0
 */
@Api(tags="用户水表")
@RestController
@RequestMapping("/arc/arcCustomers")
@Slf4j
public class ArcCustomersController extends JeecgController<ArcCustomers, IArcCustomersService> {
	@Autowired
	private IArcCustomersService arcCustomersService;

	/**
	 * 分页列表查询
	 *
	 * @param arcCustomers
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "用户水表-分页列表查询")
	@ApiOperation(value="用户水表-分页列表查询", notes="用户水表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ArcCustomers arcCustomers,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ArcCustomers> queryWrapper = QueryGenerator.initQueryWrapper(arcCustomers, req.getParameterMap());
		Page<ArcCustomers> page = new Page<ArcCustomers>(pageNo, pageSize);
		IPage<ArcCustomers> pageList = arcCustomersService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param arcCustomers
	 * @return
	 */
	@AutoLog(value = "用户水表-添加")
	@ApiOperation(value="用户水表-添加", notes="用户水表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ArcCustomers arcCustomers) {
		arcCustomersService.save(arcCustomers);
		return Result.OK("添加成功！");
	}
	/**
	 *   开户-分步添加
	 *
	 * @param cusObj
	 * @return
	 */
	@AutoLog(value = "用户水表-添加")
	@ApiOperation(value="用户水表-添加", notes="用户水表-添加")
	@PostMapping(value = "/addOpen")
	public Result<?> addOpen(@RequestBody JSONObject cusObj) throws ParseException {
		return arcCustomersService.saveAccount(cusObj);
	}
	/**
	 *   批量修改
	 *
	 * @param materObj
	 * @return
	 */
	@AutoLog(value = "批量修改")
	@ApiOperation(value="批量修改", notes="批量修改")
	@PostMapping(value = "/batchEdit")
	public Result<?> batchEdit(@RequestBody JSONObject materObj) throws ParseException {
		return arcCustomersService.batchMater(materObj);
	}
	/**
	 *   报停
	 *
	 * @param cusObj
	 * @return
	 */
	@AutoLog(value = "用户水表-报停")
	@ApiOperation(value="用户水表-报停", notes="用户水表-报停")
	@PostMapping(value = "/addNotice")
	public Result<?> addNotice(@RequestBody JSONObject cusObj) throws ParseException {
		return arcCustomersService.addNotice(cusObj);
	}
	/**
	 *   停水
	 *
	 * @param cusObj
	 * @return
	 */
	@AutoLog(value = "用户水表-停水")
	@ApiOperation(value="用户水表-停水", notes="用户水表-停水")
	@PostMapping(value = "/addStop")
	public Result<?> addStop(@RequestBody JSONObject cusObj) throws ParseException {
		return arcCustomersService.addStop(cusObj);
	}
	/**
	 *   销户
	 *
	 * @param cusObj
	 * @return
	 */
	@AutoLog(value = "用户水表-销户")
	@ApiOperation(value="用户水表-销户", notes="用户水表-销户")
	@PostMapping(value = "/addCancel")
	public Result<?> addCancel(@RequestBody JSONObject cusObj) throws ParseException {
		return arcCustomersService.addCancel(cusObj);
	}
	/**
	 *   恢复正常
	 *
	 * @param cusObj
	 * @return
	 */
	@AutoLog(value = "用户水表-恢复正常")
	@ApiOperation(value="用户水表-恢复正常", notes="用户水表-恢复正常")
	@PostMapping(value = "/addRecover")
	public Result<?> addRecover(@RequestBody JSONObject cusObj) throws ParseException {
		return arcCustomersService.addRecover(cusObj);
	}
	/**
	 *   批量修改
	 *
	 * @param cusObj
	 * @return
	 */
	@AutoLog(value = "用户水表-批量修改")
	@ApiOperation(value="用户水表-批量修改", notes="用户水表-批量修改")
	@PostMapping(value = "/addBatch")
	public Result<?> addBatch(@RequestBody JSONObject cusObj) throws ParseException {
		return arcCustomersService.addBatch(cusObj);
	}
	/**
	 *   添加时-用户编号
	 * @return
	 */
	@AutoLog(value = "用户水表-添加用户编号")
	@ApiOperation(value="用户水表-添加用户编号", notes="用户水表-添加用户编号")
	@GetMapping(value = "/addCusNo")
	public Result<?> addCusNo() {
		String nextBianCode = arcCustomersService.queryNextCode();
		return Result.OK(nextBianCode);
	}

	/**
	 *  编辑
	 *
	 * @param arcCustomers
	 * @return
	 */
	@AutoLog(value = "用户水表-编辑")
	@ApiOperation(value="用户水表-编辑", notes="用户水表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ArcCustomers arcCustomers) {
		arcCustomersService.updateById(arcCustomers);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "用户水表-通过id删除")
	@ApiOperation(value="用户水表-通过id删除", notes="用户水表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		arcCustomersService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "用户水表-批量删除")
	@ApiOperation(value="用户水表-批量删除", notes="用户水表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.arcCustomersService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "用户水表-通过id查询")
	@ApiOperation(value="用户水表-通过id查询", notes="用户水表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ArcCustomers arcCustomers = arcCustomersService.getById(id);
		if(arcCustomers==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(arcCustomers);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param arcCustomers
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ArcCustomers arcCustomers) {
        return super.exportXls(request, arcCustomers, ArcCustomers.class, "用户水表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ArcCustomers.class);
    }

}
